﻿var url;
function newInfo() {
    $('#dlg').dialog('open').dialog('setTitle', 'New User');
    $('#fm').form('clear');
    url = 'save_user.php';
}
function editInfo() {
    
    $.messager.alert("提示", "请先选中一条记录");
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', 'Edit User');
        $('#fm').form('load', row);
        url = 'update_user.php?id=' + row.id;
    }
}
function saveInfo() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            var result = eval('(' + result + ')');
            if (result.errorMsg) {
                $.messager.show({
                    title: 'Error',
                    msg: result.errorMsg
                });
            } else {
                $('#dlg').dialog('close'); // close the dialog
                $('#dg').datagrid('reload'); // reload the user data
            }
        }
    });
}

/**
 * 删除记录
 */
function destroyInfo() {
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        $.messager.confirm('删除信息', '<center style=height:50px;line-height:50px;>确定删除？</center>', function (r) {
            if (r) {
                $.post('destroy_user.php', { id: row.id }, function (result) {
                    if (result.success) {
                        $('#dg').datagrid('reload'); // reload the user data
                    } else {
                        $.messager.show({ // show error message
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    }
                }, 'json');
            }
        });
    }
}
